package com.geovis.emergency.spd.controller.pc;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.geovis.common.core.api.Result;
import com.geovis.common.core.controller.BaseController;
import com.geovis.emergency.spd.biz.basic.service.IBasicDutyOrgService;
import com.geovis.emergency.spd.biz.pc.service.*;
import com.geovis.emergency.spd.entity.pc.entity.PcDrillDutyOrg;
import com.geovis.emergency.spd.entity.pc.entity.PcDrillPlanDetail;
import com.geovis.emergency.spd.entity.pc.pojo.dto.PcDrillDutyOrganDTO;
import com.geovis.emergency.spd.entity.pc.pojo.dto.PcDrillDutyStatisticDTO;
import com.geovis.emergency.spd.entity.pc.pojo.dto.PcDutyImtDTO;
import com.geovis.emergency.spd.entity.pc.pojo.enums.StatusEnum;
import com.geovis.emergency.spd.entity.pc.pojo.vo.PcDrillDutyOrgVO;
import com.geovis.emergency.spd.entity.pc.pojo.vo.RollCallUserVO;
import com.geovis.emergency.spd.entity.pc.pojo.vo.StringVO;
import com.geovis.emergency.spd.entity.pc.pojo.vo.TreeVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 应急预案指挥模块-值班排班表 前端控制器
 * </p>
 *
 * @author 曾德实
 * @since 2022-04-30
 */
@RestController
@RequestMapping("/pc/pcDuty")
@RequiredArgsConstructor
@Api(value = "应急预案指挥模块-值班排班表接口", tags = "应急预案指挥模块-值班排班表接口")
@Slf4j
@Validated
public class PcDutyController extends BaseController<IPcDutyService> {

    private final IBasicDutyOrgService basicDutyOrgService;

    private final IPcRollCallService pcRollCallService;
    private final IPcDrillDutyOrgService pcDrillDutyOrgService;
    private final IPcDrillDutyUserService pcDrillDutyUserService;

    private final IPcDrillPlanDetailService pcDrillPlanDetailService;


    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "导入值班表", notes = "导入值班表")
    @PostMapping({"/imt"})
    public Result<String> imt(PcDutyImtDTO imtDTO) {
        baseService.imt(imtDTO);
        return Result.ok();
    }

    @ApiOperation(value = "获取机构点名树", notes = "获取机构点名树")
    @GetMapping({"/getOrgCallTree"})
    public Result<List<PcDrillDutyOrgVO>> getOrgCallTree(String pcDrillId) {
        return Result.ok(baseService.getOrgCallTree(pcDrillId));
    }


    @Deprecated
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "指挥编组查询", notes = "指挥编组查询")
    @PostMapping({"/getGroupByDrillId/{drillId}"})
    public Result getGroupByDrillId(@PathVariable("drillId") String drillId) {
        List<PcDrillDutyStatisticDTO> list = baseService.getGroupByDrillId(drillId);
        return Result.ok(list);
    }
    @Deprecated
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "值班机构", notes = "值班机构")
    @PostMapping({"/getDutyOrganByDrillId/{drillId}"})
    public Result getDutyOrganByDrillId(@PathVariable("drillId") String drillId) {
        List<PcDrillDutyOrganDTO> list = baseService.getDutyOrganByDrillId(drillId);
        return Result.ok(list);
    }

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "值班编组用户", notes = "值班编组用户")
    @PostMapping({"/getDutyUserByDrillId/{drillId}"})
    public Result<List<TreeVO>> getDutyUser(@PathVariable("drillId") String drillId,
                                            @RequestParam(value = "drill",required = false) String drill){
        List<String> groupList = Lists.newArrayList();
        if (StatusEnum.TRUE.getCode().equals(drill)){
            groupList = pcDrillDutyOrgService.myDutyGroupList(drillId,true);
        }
        return Result.ok(baseService.dutyTree(drillId,groupList,true));
    }
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "值班编组点名列表", notes = "值班编组点名列表")
    @PostMapping({"/getCallRollDutyUser/{drillId}"})
    public Result<RollCallUserVO> callRollDutyUser(@PathVariable("drillId") String drillId){
        return Result.ok(pcRollCallService.callRollDutyUser(drillId));
    }
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "值班编组", notes = "值班编组")
    @PostMapping({"/dutyGroup/{pcDrillId}"})
    public Result<List<StringVO>> groupList(@PathVariable("pcDrillId") String pcDrillId){
        return Result.ok(pcDrillDutyOrgService.dutyGroupList(pcDrillId));
    }
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "我所在值班编组", notes = "我所在值班编组")
    @PostMapping({"/myDutyGroup/{drillId}"})
    public Result<List<String>> myDutyGroup(@PathVariable("drillId") String drillId){

        return Result.ok(pcDrillDutyOrgService.myDutyGroupList(drillId,false));
    }
}
